#include "Game.h"
#include <map>
#include <string>

std::vector<Unit> Game::Enemies;
Unit Game::Player(100, 50, 50, 2);
std::vector<Projectile> Game::Projectiles;
D3DEngine* Game::Engine;
UINT Game::Time, Game::Period, Game::LastClick, Game::LastRClick, Game::Wave = 0, Game::BuildingState=0;
std::vector<Sprite> Game::Floor;
std::vector<Tower> Game::Towers;
D3DXVECTOR2 Game::Target, Game::TargetTile;
Tower Game::CurrentBuilding(2, 1, 0, 25, 1, 4);
int Game::FPS;
int Game::AvgFPS, Game::Gold = 50;

static Mesh NavMesh;
static Vector2 start, finish;
static std::vector<Vector2> Path;

static bool Grid[MAP_WIDTH / TILE][MAP_HEIGHT / TILE];
static Tile Grid2[2 + 2 * MAP_WIDTH / TILE][2 + 2 * MAP_HEIGHT / TILE];

static int Lives = 30;

static void FindCorners()
{
	for (int i = 0; i < 2 + 2 * MAP_WIDTH / TILE; i++)
	{
		for (int j = 0; j < 2 + 2 * MAP_HEIGHT / TILE; j++)
		{
			Grid2[i][j].Corner = false;
		}
	}
	for (int i = 0; i < MAP_WIDTH / TILE; i++)
	{
		for (int j = 0; j < MAP_HEIGHT / TILE; j++)
		{
			if (Grid[i][j])
			{
				for (int k = -1; k <= 1; k += 2)
				{
					for (int l = -1; l <= 1; l += 2)
					{
						if (i + k >= 0 && i + k <= MAP_WIDTH / TILE && j + l >= 0 && j + l <= MAP_HEIGHT / TILE)
						{
							if (Grid[i + k][j + l] && !Grid[i][j + l])
							{
								int cx = 1 + 2 * i + (k + 1) / 2, cy = 2 * j + 3 * (l + 1) / 2;
								Grid2[cx][cy].Corner = true;
								Grid2[cx][cy].DirectionIn = (((l + 1) / 2) << 1) + (k + 1) / 2;
								Grid2[cx][cy].DirectionOut = ((!((k + 1) / 2) & 1) << 1) + (l + 1) / 2;
							}
							else if (!Grid[i + k][j + l] && !Grid[i][j + l] && !Grid[i + k][j])
							{
								int cx = 2 * i + 3 * (k + 1) / 2, cy = 2 * j + 3 * (l + 1) / 2;
								Grid2[cx][cy].Corner = true;
								Grid2[cx][cy].DirectionIn = (((k + 1) / 2) << 1) + (l + 1) / 2;
								Grid2[cx][cy].DirectionOut = (((l + 1) / 2) << 1) + (!((k + 1) / 2) & 1);
							}
						}
						else if (i + k >= 0 && i + k <= MAP_WIDTH / TILE)
						{
							if (!Grid[i + k][j])
							{
								int cx = 2 * i + 3 * (k + 1) / 2, cy = 2 * j + 3 * (l + 1) / 2;
								Grid2[cx][cy].Corner = true;
								Grid2[cx][cy].DirectionIn = (((k + 1) / 2) << 1) + (l + 1) / 2;
								Grid2[cx][cy].DirectionOut = (((l + 1) / 2) << 1) + (!((k + 1) / 2) & 1);
							}
						}
						else if (j + l >= 0 && j + l <= MAP_HEIGHT / TILE)
						{
							if (!Grid[i][j + l])
							{
								int cx = 2 * i + 3 * (k + 1) / 2, cy = 2 * j + 3 * (l + 1) / 2;
								Grid2[cx][cy].Corner = true;
								Grid2[cx][cy].DirectionIn = (((k + 1) / 2) << 1) + (l + 1) / 2;
								Grid2[cx][cy].DirectionOut = (((l + 1) / 2) << 1) + (!((k + 1) / 2) & 1);
							}
						}
						else
						{
							int cx = 2 * i + 3 * (k + 1) / 2, cy = 2 * j + 3 * (l + 1) / 2;
							Grid2[cx][cy].Corner = true;
							Grid2[cx][cy].DirectionIn = (((k + 1) / 2) << 1) + (l + 1) / 2;
							Grid2[cx][cy].DirectionOut = (((l + 1) / 2) << 1) + (!((k + 1) / 2) & 1);
						}
					}
				}
			}
		}
	}
}

void UpdateHoles(std::vector<Poly> *Holes)
{
	for (std::vector<Poly>::iterator it = Holes->begin(); it != Holes->end(); ++it)
	{
		it->Delete();
	}
	Holes->clear();
	for (int i = 0; i < 2 + 2 * MAP_WIDTH / TILE; i++)
	{
		for (int j = 0; j < 2 + 2 * MAP_HEIGHT / TILE; j++)
		{
			if (Grid2[i][j].Corner)
			{
				std::vector<Vector2> Points;
				Points.push_back(Vector2((i - .5f) * TILE / 2 - MAP_WIDTH / 2, (j - .5f) * TILE / 2 - MAP_HEIGHT / 2));
				Grid2[i][j].Corner = false;
				Tile current = Grid2[i][j];
				int k = 0, l = 0;
				while (true)
				{
					if (current.DirectionOut == 0) k--;
					else if (current.DirectionOut == 1) 
						l++;
					else if (current.DirectionOut == 2) 
						l--;
					else if (current.DirectionOut == 3) k++;
					if (k == 0 && l == 0) break;
					if (Grid2[i + k][j + l].Corner && Grid2[i + k][j + l].DirectionIn == current.DirectionOut)
					{
						Points.push_back(Vector2((i + k - .5f) * TILE / 2 - MAP_WIDTH / 2, (j + l - .5f) * TILE / 2 - MAP_HEIGHT / 2));
						Grid2[i + k][j + l].Corner = false;
						current = Grid2[i + k][j + l];
					}
				}
				Holes->push_back(Poly());
				for (std::vector<Vector2>::iterator it = Points.begin(); it != Points.end(); it++)
				{
					Holes->back().Append(*it);
				}
			}
		}
	}
}

void Game::init(D3DEngine* e)
{
	Engine = e;
	Wave = 0;
	Player = Unit(100, 50, 50, 2);
	Player.x = 0;
	Player.y = 0;
	Player.speed = 3.0f;
	//SpawnEnemies();
	Time = timeGetTime();
	LastClick = Time;
	LastRClick = LastClick;

	LPCWSTR TexturesArray[] =
	{ 
		L"\\Images\\Chess.png",		//0
		L"\\Images\\Tower.png",		//1
		L"\\Images\\Player.png",	//2
		L"\\Images\\Enemy.png",		//3
		L"\\Images\\Triangle.png",	//4
		L"\\Images\\Bar.png"		//5
	};

	Engine->LoadTextures(TexturesArray, 6);

	D3DSURFACE_DESC Desc;
	Engine->Textures[0]->GetLevelDesc(0, &Desc);
	
	for (int i = Desc.Width / 2; i <= MAP_WIDTH - Desc.Width / 2; i += Desc.Width)
	{
		for (int j = Desc.Height / 2; j <= MAP_HEIGHT - Desc.Height / 2; j += Desc.Height)
		{
			Floor.push_back(Sprite(0));
			Floor.back().x = (float)i - MAP_WIDTH / 2;
			Floor.back().y = (float)j - MAP_HEIGHT / 2;
		}
	}

	start = Vector2(-MAP_WIDTH / 2 + 50, 0);
	finish = Vector2(MAP_WIDTH / 2 - 50, 0);

	Vector2 Edges[] = { Vector2(-MAP_WIDTH / 2, -MAP_HEIGHT / 2), Vector2(-MAP_WIDTH / 2, MAP_HEIGHT / 2), Vector2(MAP_WIDTH / 2, MAP_HEIGHT / 2), Vector2(MAP_WIDTH / 2, -MAP_HEIGHT / 2) }; 
	NavMesh = Mesh(Poly(Edges, 4));
	NavMesh.UpdateGraph();
	NavMesh.GetPath(start, finish, &Path);
	FPS = 0;
	Period = 0;
}

void Game::Update(MSG msg, POINT Cursor)
{
	UINT t = timeGetTime();
	UINT DT = t - Time;
	Period += DT;
	if (Period >= 2000)
	{
		FPS = 1000 / DT;
		Period -= 2000;
	}
	Unit* Closest;
	Target = D3DXVECTOR2(Player.x + Cursor.x - SCREEN_WIDTH / 2, Player.y + Cursor.y - SCREEN_HEIGHT / 2);
	TargetTile = D3DXVECTOR2(floor(Target.x / TILE) * TILE + TILE / 2.0f, floor(Target.y / TILE) * TILE + TILE / 2.0f);

	bool k1 = GetAsyncKeyState(0x31) & 0x8000;
	bool k2 = GetAsyncKeyState(0x32) & 0x8000;
	bool k3 = GetAsyncKeyState(0x33) & 0x8000;
	bool click = GetAsyncKeyState(VK_LBUTTON) & 0x8000; 
	bool esc = GetAsyncKeyState(VK_ESCAPE) & 0x8000;

	if (!k1 && BuildingState & (1 << 2))
	{
		BuildingState &= !0x3;
		BuildingState |= 0x1;
		CurrentBuilding = Tower(2, 1, 0, 25, 1, 4);
		CurrentBuilding.Color = 0xFFFFFFFF;
		CurrentBuilding.Range = 300.0f;
	}
	else if (!k2 && BuildingState & (1 << 3)) 
	{
		BuildingState &= !0x3;
		BuildingState |= 0x1;
		CurrentBuilding = Tower(3, .5, 100, 50, 1, 4);
		CurrentBuilding.Color = 0xFFFF0000;
		CurrentBuilding.Range = 300.0f;
	}
	else if (!k3 && BuildingState & (1 << 4)) 
	{
		BuildingState &= !0x3;
		BuildingState |= 0x3;
		CurrentBuilding = Tower(1, 1, 100, 50, 1, 4);
		CurrentBuilding.Color = 0xFF0000FF;
		CurrentBuilding.Range = 300.0f;
	}

	if (k1) BuildingState |= (1 << 2);
	if (k2) BuildingState |= (1 << 3);
	if (k3) BuildingState |= (1 << 4);

	if (BuildingState & 0x3)
	{
		CurrentBuilding.x = TargetTile.x;
		CurrentBuilding.y = TargetTile.y;
		if (esc)
		{
			BuildingState &= !0x3;
		}
		else if (!(BuildingState & (1 << 5)))
		{
			if (click)
			{
				bool TileEmpty = true;
				if (!Enemies.empty())
				{
					for (std::vector<Unit>::iterator it = Enemies.begin(); it != Enemies.end(); ++it)
					{
						Unit* p = &Enemies[it - Enemies.begin()];
						if (p->IntersectsWith(&CurrentBuilding))
						{
							TileEmpty = false;
							break;
						}
					}
				}
				if (!Towers.empty())
				{
					for (std::vector<Tower>::iterator it = Towers.begin(); it != Towers.end(); ++it)
					{
						Tower* p = &Towers[it - Towers.begin()];
						if (p->IntersectsWith(&CurrentBuilding))
						{
							TileEmpty = false;
							break;
						}
					}
				}
				if (TileEmpty && Gold >= CurrentBuilding.Cost)
				{
					Gold -= CurrentBuilding.Cost;
					Towers.emplace_back(CurrentBuilding);
					BuildingState &= !0x3;
					BuildingState |= (1 << 5);
					Grid[((int)TargetTile.x + MAP_WIDTH / 2)/TILE][((int)TargetTile.y + MAP_HEIGHT / 2)/TILE] = true;
					FindCorners();
					UpdateHoles(NavMesh.GetHoles());
					NavMesh.UpdateGraph();
					NavMesh.GetPath(start, finish, &Path);
					NavMesh.GetPath(start, finish, &Path);
					for (std::vector<Unit>::iterator it = Enemies.begin(); it != Enemies.end(); ++it)
					{
						NavMesh.GetPath(Vector2(it->x, it->y), finish, &it->Path);
						it->path_index = 0;
					}
				}
			}
		}
		else
		{
			if (!click) BuildingState &= !(1 << 5);
		}
	}

	bool up = GetAsyncKeyState(VK_UP) & 0x8000 || GetAsyncKeyState(0x57) & 0x8000;
	bool down = GetAsyncKeyState(VK_DOWN) & 0x8000 || GetAsyncKeyState(0x53) & 0x8000;
	bool left = GetAsyncKeyState(VK_LEFT) & 0x8000 || GetAsyncKeyState(0x41) & 0x8000;
	bool right = GetAsyncKeyState(VK_RIGHT) & 0x8000 || GetAsyncKeyState(0x44) & 0x8000;

	if (up && !down) Player.VelocityY = -1;
	else if (down && !up) Player.VelocityY = 1;
	else Player.VelocityY = 0;

	if (right && !left) Player.VelocityX = 1;
	else if (left && !right) Player.VelocityX = -1;
	else Player.VelocityX = 0;

	if (Player.VelocityX != 0 && Player.VelocityY != 0)
	{
		Player.VelocityX = Player.VelocityX * sqrt(2) / 2;
		Player.VelocityY = Player.VelocityY * sqrt(2) / 2;
	}

	//if (t - LastClick > 300)
	//{
	//	if (GetAsyncKeyState(VK_LBUTTON) & 0x8000)
	//	{
	//		Projectiles.emplace_back(2, 20, 20, ProjectileTexturePath.c_str());
	//		Projectiles.back().x = Player.x + 10 * cos(Player.r);
	//		Projectiles.back().y = Player.y + 10 * sin(Player.r);
	//		Projectiles.back().r = Player.r;
	//		Projectiles.back().Owner = &Player;
	//		LastClick = t;
	//	}
	//}

	//if (t - LastRClick > 1500)
	//{
	//	if (GetAsyncKeyState(VK_RBUTTON) & 0x8000)
	//	{
	//		Projectiles.emplace_back(8, 40, 40, ProjectileTexturePath.c_str());
	//		Projectiles.back().x = Player.x + 10 * cos(Player.r);
	//		Projectiles.back().y = Player.y + 10 * sin(Player.r);
	//		Projectiles.back().r = Player.r;
	//		Projectiles.back().Owner = &Player;
	//		Projectiles.back().AoERange = 200;
	//		LastRClick = t;
	//	}
	//}

	Player.x += Player.VelocityX * Player.speed * DT / 10;
	Player.y += Player.VelocityY * Player.speed * DT / 10;

	/*if (Player.x > MAP_WIDTH / 2) Player.x = MAP_WIDTH / 2;
	else if (Player.x < -MAP_WIDTH / 2) Player.x = -MAP_WIDTH / 2;
	if (Player.y > MAP_HEIGHT / 2) Player.y = MAP_HEIGHT / 2;
	else if (Player.y < -MAP_HEIGHT / 2) Player.y = -MAP_HEIGHT / 2;*/
	
	Player.r = atan2(Cursor.y - SCREEN_HEIGHT / 2, Cursor.x - SCREEN_WIDTH / 2);

	for (std::vector<Projectile>::iterator it = Projectiles.begin(); it != Projectiles.end();)
	{
		bool hit = false;
		Projectile* p = &Projectiles[it - Projectiles.begin()];
		if (p->distance > p->Range)
		{
			hit = true;
			it = Projectiles.erase(it);
		}
		else if (!Enemies.empty())
		{
			Unit* ClosestUnit = NULL;
			float min = INFINITY;
			float d;
			for (int j = 0; j < Enemies.size(); j++)
			{
				d = p->DistanceFrom(&Enemies[j]);
				if (d < min)
				{
					min = d;
					ClosestUnit = &Enemies[j];
				}
			}
			if (p->IntersectsWith(ClosestUnit))
			{
				hit = true;
				if (p->AoERange>0.0f)
				{
					for (int j = 0; j < Enemies.size(); j++)
					{
						if (p->DistanceFrom(&Enemies[j]) < p->AoERange) Enemies[j].HP -= p->Damage;
					}
				}
				else ClosestUnit->HP -= p->Damage;
				it = Projectiles.erase(it);
			}
		}
		if (!hit)
		{
			float dx = p->speed * cos(p->r) * DT / 10;
			float dy = p->speed * sin(p->r) * DT / 10;
			p->x += dx;
			p->y += dy;
			p->distance += sqrt(dx*dx + dy*dy);
			it++;
		}
	}

	if (!Towers.empty())
	{
		for (std::vector<Tower>::iterator it = Towers.begin(); it != Towers.end(); ++it)
		{
			if (t - it->LastAttack > HIT_TIME / it->AtackSpeed && !Enemies.empty())
			{
				Unit* pTarget = &Enemies.front();
				bool hasTarget = false;
				for (std::vector<Unit>::iterator jt = Enemies.begin(); jt != Enemies.end(); ++jt)
				{
					if (jt->DistanceFrom(Vector2(it->x, it->y)) <= it->Range)
					{
						hasTarget = true;
						pTarget = &*jt;
						break;
					}
				}
				if (hasTarget)
				{
					Projectiles.emplace_back(it->Damage, 10, 10, it->Range, it->AoERange, it->HitTextureId);
					Projectiles.back().x = it->x;
					Projectiles.back().y = it->y;
					Projectiles.back().r = atan2(pTarget->y - it->y, pTarget->x - it->x);
					Projectiles.back().Owner = &*it;
					it->LastAttack = t;
				}
			}
		}
	}
	
	if (!Enemies.empty())
	{
		Closest = &Enemies.front();
		for (std::vector<Unit>::iterator it = Enemies.begin(); it != Enemies.end();)
		{
			Unit* c = &Enemies[it - Enemies.begin()];
			c->Targeted = false;
			c->Closest = false;
			if (c->HP <= 0)
			{
				Gold += 5;
				it = Enemies.erase(it);
			}
			else
			{
				float dx = c->x - c->Path[c->path_index].x, dy = c->y - c->Path[c->path_index].y;
				if (dy >= -1.0f && dy <= 1.0f && dx >= -1.0f && dx <= 1.0f)
				{
					if (c->path_index == c->Path.size() - 1)
					{
						Lives--;
						it = Enemies.erase(it);
						continue;
					}
					c->path_index++;
				}
				if (c->DistanceFrom(Target) < Closest->DistanceFrom(Target))
				{
					Closest = c;
				}
				c->r = atan2(c->Path[c->path_index].y - c->y, c->Path[c->path_index].x - c->x);
				c->x += c->speed * cos(c->r) * DT / 10;
				c->y += c->speed * sin(c->r) * DT / 10;
				it++;
			}

		}
		bool ix = false;
		bool iy = false;
		if (Closest->x > Target.x)
		{
			if (Closest->x - Closest->HitBoxW / 2 < Target.x) ix = true;
		}
		else
		{
			if (Closest->x + Closest->HitBoxW / 2 > Target.x) ix = true;
		}
		if (Closest->y > Target.y)
		{
			if (Closest->y - Closest->HitBoxH / 2 < Target.y) iy = true;
		}
		else
		{
			if (Closest->y + Closest->HitBoxH / 2 > Target.y)
				iy = true;
		}
		if (ix && iy) Closest->Targeted = true;
		Closest->Closest = true;
	}
	else SpawnEnemies();
	Time = t;
}

void Game::Draw()
{
	Engine->SetView(D3DXVECTOR2(Player.x, Player.y));

	for (std::vector<Sprite>::iterator it = Floor.begin(); it != Floor.end(); it++)
	{
		Engine->DrawSprite(&Floor[it - Floor.begin()], false);
	}

	//Engine->DrawLineW(Vector2(-1000.0f, 0), Vector2(1000.0f, 0), 0xFFFFFFFF);

	//Graph* nGraph = NavMesh.GetNavGraph();
	//std::vector<Vertex*> gVertices = nGraph->GetVertices();
	//for (std::vector<Vertex*>::iterator it = gVertices.begin(); it != gVertices.end(); ++it)
	//{
	//	Engine->DrawCircleW((*it)->data, 10.0f, 0xFF00FF00);
	//	for (std::vector<Edge>::iterator jt = (*it)->Edges.begin(); jt != (*it)->Edges.end(); ++jt)
	//	{
	//		Engine->DrawCircleW(jt->Origin->data, 8.0f, 0xFF0000FF);
	//		Engine->DrawLineW(jt->Origin->data, jt->Destination->data, 0xFF00FF00);
	//	}
	//}

	//for (std::vector<Poly>::iterator it = NavMesh.GetHoles()->begin(); it != NavMesh.GetHoles()->end(); ++it)
	//{
	//	PolyVertex* a = it->First();
	//	Engine->DrawCircleW(a->pos, 5.0f, 0xFF00FFFF);
	//	do
	//	{
	//		//if (NavMesh.GetBorder()->ContainsPoint(a->pos)) Engine->DrawCircleW(a->pos, 15.0f, 0xFF00FF00);
	//		//Engine->DrawCircleW(a->pos, 10.0f, 0xFF00FFFF);
	//		//Engine->DrawLineW(a->pos, a->next->pos, 0xFF00FFFF);
	//		//Engine->DrawCircleW(a->next->pos, 20.0f, 0xFF00FFFF);
	//		Vector2 v1 = a->pos - a->next->pos;
	//		//Engine->DrawLineW(a->next->pos, a->next->pos + 0.3f * v1, 0xFFFF0000);
	//		a = a->next;
	//	} while (a != it->First());
	//}
	PolyVertex* a = NavMesh.GetBorder()->First();
	do
	{
		//Engine->DrawCircleW(a->pos, 10.0f, 0xFF00FFFF);
		Engine->DrawLineW(a->pos, a->next->pos, 0xFF00FFFF);
		//Engine->DrawCircleW(a->next->pos, 20.0f, 0xFF00FFFF);
		a = a->next;
	} while (a != NavMesh.GetBorder()->First());

	Engine->DrawCircleW(start, 10.0f, 0xFF0000FF);
	Engine->DrawCircleW(finish, 10.0f, 0xFF0000FF);
	if (Path.size() > 1)
	{
		for (std::vector<Vector2>::iterator it = Path.begin(); it != Path.end() - 1; ++it)
		{
			Engine->DrawLineW(*it, *(it + 1), 0xFF0000FF);
		}
	}

	for (std::vector<Unit>::iterator it = Enemies.begin(); it != Enemies.end(); it++)
	{
		Unit* p = &Enemies[it - Enemies.begin()];
		Engine->DrawSprite(p, false);
		
		//HitBox
		if (p->Targeted) Engine->DrawRect(D3DXVECTOR2(p->x - p->HitBoxW / 2 + SCREEN_WIDTH / 2 - Player.x, p->y - p->HitBoxH / 2 + SCREEN_HEIGHT / 2 - Player.y), D3DXVECTOR2(p->x + p->HitBoxW / 2 + SCREEN_WIDTH / 2 - Player.x, p->y + p->HitBoxH / 2 + SCREEN_HEIGHT / 2 - Player.y), 0xFF00FFFF);
		else if (p->Closest) Engine->DrawRect(D3DXVECTOR2(p->x - p->HitBoxW / 2 + SCREEN_WIDTH / 2 - Player.x, p->y - p->HitBoxH / 2 + SCREEN_HEIGHT / 2 - Player.y), D3DXVECTOR2(p->x + p->HitBoxW / 2 + SCREEN_WIDTH / 2 - Player.x, p->y + p->HitBoxH / 2 + SCREEN_HEIGHT / 2 - Player.y), 0xFFFF0000);
		else Engine->DrawRect(D3DXVECTOR2(p->x - p->HitBoxW / 2 + SCREEN_WIDTH / 2 - Player.x, p->y - p->HitBoxH / 2 + SCREEN_HEIGHT / 2 - Player.y), D3DXVECTOR2(p->x + p->HitBoxW / 2 + SCREEN_WIDTH / 2 - Player.x, p->y + p->HitBoxH / 2 + SCREEN_HEIGHT / 2 - Player.y), 0xFF0000FF);
		
		//HealthBar
		Engine->DrawRect(D3DXVECTOR2(p->x - p->HitBoxW / 2 + SCREEN_WIDTH / 2 - Player.x, p->y - p->HitBoxH / 2 - 21.0f + SCREEN_HEIGHT / 2 - Player.y), D3DXVECTOR2(p->x + p->HitBoxW / 2 + SCREEN_WIDTH / 2 - Player.x, p->y - p->HitBoxH / 2  - 1.0f + SCREEN_HEIGHT / 2 - Player.y), 0xFFFF0000);
		p->UpdateHealthBar();
		Engine->DrawSprite(&p->HealthBar, false);
	}
	for (std::vector<Projectile>::iterator it = Projectiles.begin(); it != Projectiles.end(); it++)
	{
		Projectile* p = &Projectiles[it - Projectiles.begin()];
		Engine->DrawSprite(p, false);
		Engine->DrawRect(D3DXVECTOR2(p->x - p->HitBoxW / 2 + SCREEN_WIDTH / 2 - Player.x, p->y - p->HitBoxH / 2 + SCREEN_HEIGHT / 2 - Player.y), D3DXVECTOR2(p->x + p->HitBoxW / 2 + SCREEN_WIDTH / 2 - Player.x, p->y + p->HitBoxH / 2 + SCREEN_HEIGHT / 2 - Player.y), 0xFF0000FF);
	}
	if (BuildingState & 0x3)
	{
		Engine->DrawSprite(&CurrentBuilding, false);
	}
	for (std::vector<Tower>::iterator it = Towers.begin(); it != Towers.end(); it++)
	{
		Engine->DrawSprite(&Towers[it - Towers.begin()], false);
		//Engine->DrawCircle(D3DXVECTOR2(Towers[it - Towers.begin()].x + SCREEN_WIDTH / 2 - Player.x, Towers[it - Towers.begin()].y + SCREEN_HEIGHT / 2 - Player.y), 200.0f, 0xFF0000FF);
	}
	Engine->DrawRect(D3DXVECTOR2(TargetTile.x - TILE / 2 + SCREEN_WIDTH / 2 - Player.x, TargetTile.y - TILE / 2 + SCREEN_HEIGHT / 2 - Player.y), D3DXVECTOR2(TargetTile.x + TILE / 2 + SCREEN_WIDTH / 2 - Player.x, TargetTile.y + TILE / 2 + SCREEN_HEIGHT / 2 - Player.y), 0xFF00FF00);
	//Engine->DrawSprite(&Player, false);

	std::wstring fps_str = std::to_wstring(FPS);
	Engine->DrawString(fps_str, Vector2(50, 30), 0xFF00FF00);

	Engine->DrawString(std::to_wstring(Lives), Vector2(50, 50), 0xFFFF0000);

	Engine->DrawString(std::to_wstring(Wave), Vector2(50, 70), 0xFFFFFFFF);

	Engine->DrawString(std::to_wstring(Gold), Vector2(50, 90), 0xFFFFFF00);
}

void Game::SpawnEnemies()
{
	Wave++;
	for (int i = 0; i < Wave; i++)
	{
		Enemies.emplace_back(10 + 5 * (Wave / 5) * (Wave / 5), 30, 30, 3);
		Enemies.back().x = - MAP_WIDTH / 2;
		Enemies.back().y = - MAP_HEIGHT / 2 + rand() % MAP_HEIGHT;
		Enemies.back().speed = .5f;
		Enemies.back().Path = Path;
		Enemies.back().path_index = 0;
	}
}